public class Individual {

    static int defaultGeneLength = 20;
    private byte[] genes = new byte[defaultGeneLength];
    // Cache
    private int fitness = 0;
    
    // diametre de deuxieme cercle
    double d1;
    // diametre de deuxieme cercle
    double d2;
    // diametre de troiseme cercle
    double d3;

    // Create a random individual
    public void generateIndividual() {
        for (int i = 0; i < size(); i++) {
            byte gene = (byte) Math.round(Math.random());
            genes[i] = gene;
        }
    }

    /* Getters and setters */
    // Use this if you want to create individuals with different gene lengths
    public static void setDefaultGeneLength(int length) {
        defaultGeneLength = length;
    }
    
    public byte getGene(int index) {
        return genes[index];
    }

    public void setGene(int index, byte value) {
        genes[index] = value;
        fitness = 0;
    }

    public double getD1() {
		return d1;
	}

	public void setD1(double d1) {
		this.d1 = d1;
	}

	public double getD2() {
		return d2;
	}

	public void setD2(double newD2) {
		this.d2 = newD2;
	}

	public double getD3() {
		return d3;
	}

	public void setD3(double d3) {
		this.d3 = d3;
	}

	/* Public methods */
    public int size() {
        return genes.length;
    }

    public int getFitness() {
        if (fitness == 0) {
            fitness = FitnessCalc.getFitness(this);
        }
        return fitness;
    }
    
    /**
     * Decodage
     */
    
    public void decodage() {
    	String strd2 = "";
    	String strd3 = "";
    	
    	for (int i = 0; i < 10; i++) {
			 strd2 += this.getGene(i);
		}
    	
    	for (int i = 10; i < 20; i++) {
			 strd3 += this.getGene(i);
		}
    	
    	int valgenD2 = Integer.parseInt(strd2, 2);
    	int valgenD3 = Integer.parseInt(strd3, 2);
    	double newD2 = 10 + valgenD2*((80 - 10)/Math.pow(2, 10));
    	double newD3 = 10 + valgenD3*((90 - this.getD2() - 10)/Math.pow(2, 10));
    	setD2(newD2);
    	setD3(newD3);
    	setD1(100 - this.getD2() - this.getD3());
	}
    
    @Override
    public String toString() {
        String geneString = "";
        for (int i = 0; i < size(); i++) {
            geneString += getGene(i);
        }
        return geneString;
    }
}